package com.integral.xr.module.platform.service.village;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.integral.xr.module.platform.controller.admin.village.vo.*;
import com.integral.xr.module.platform.dal.dataobject.village.VillageDO;
import com.integral.xr.framework.common.pojo.PageResult;
import com.integral.xr.framework.common.pojo.PageParam;
import com.integral.xr.framework.common.util.object.BeanUtils;

import com.integral.xr.module.platform.dal.mysql.village.VillageMapper;

import static com.integral.xr.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.integral.xr.module.platform.enums.ErrorCodeConstants.*;

/**
 * 村情展示 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class VillageServiceImpl implements VillageService {

    @Resource
    private VillageMapper villageMapper;

    @Override
    public String createVillage(VillageSaveReqVO createReqVO) {
        // 插入
        VillageDO village = BeanUtils.toBean(createReqVO, VillageDO.class);
        villageMapper.insert(village);
        // 返回
        return village.getId();
    }

    @Override
    public void updateVillage(VillageSaveReqVO updateReqVO) {
        // 校验存在
        validateVillageExists(updateReqVO.getId());
        // 更新
        VillageDO updateObj = BeanUtils.toBean(updateReqVO, VillageDO.class);
        villageMapper.updateById(updateObj);
    }

    @Override
    public void deleteVillage(String id) {
        // 校验存在
        validateVillageExists(id);
        // 删除
        villageMapper.deleteById(id);
    }

    private void validateVillageExists(String id) {
        if (villageMapper.selectById(id) == null) {
            throw exception(VILLAGE_NOT_EXISTS);
        }
    }

    @Override
    public VillageDO getVillage(String id) {
        return villageMapper.selectById(id);
    }

    @Override
    public PageResult<VillageDO> getVillagePage(VillagePageReqVO pageReqVO) {
        return villageMapper.selectPage(pageReqVO);
    }

}